package com.jxc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.jxc.entity.TbRole;
import com.jxc.dao.OpControlDAO;
import com.jxc.dao.PermissionDAO;
import com.jxc.dao.role;
import com.jxc.dao.impl.OpControlImpl;
import com.jxc.pojo.Combo;
import com.opensymphony.xwork2.ActionContext;

/**
 * @包名   com.jxc.web
 * @文件名 OpControlAction.java
 * @作者   mopzyqing
 * @创建日期 Jul 13, 2010
 * @版本 V 1.0
 */
public class OpControlAction {

	@Resource private OpControlDAO opControl;
	@Resource private role roleService;

	private String op;
	private int limit;  
	private int start;
	private List root = new ArrayList(); //返回给客户端的节点对象
	private int totalProperty;
	public void initOp(){
		
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);
		
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		
		String opJson = getOp();
		boolean flat = opControl.initOp(opJson);
		
		String json="";
		if(flat){
			json = "{success:true}";
		}else{
			json = "{success:false}";
		}
		try {
			
			response.getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获取所有角色
	private int flat=0;
	public String getAllRole(){
		
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);

		totalProperty=roleService.QueryRoleTotalRecord();
		if(flat!=0){
			return "success";
		}
		if(flat==0){
			flat++;
		}
		if(start>0){
			start = start/limit;
		}
		System.out.println(start+"    "+limit);
		List<TbRole> tbRoleList =  roleService.QueryRoleListForPage(start,limit);
		setRoot(new ArrayList());
		for(int i=0;i<tbRoleList.size();i++){
			
			String text = tbRoleList.get(i).getName();
			String value = tbRoleList.get(i).getRoleId().toString();
			
			List roleComboBox = new ArrayList();
			Combo combos = new Combo();
//			roleComboBox.add(value);
//			roleComboBox.add(text);
			combos.setText(text);
			combos.setValue(value);
			//root.add(roleComboBox);
			root.add(combos);
		}
		return "success";
	}
	public String getOp() {
		return op;
	}
	public void setOp(String op) {
		this.op = op;
	}
	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}
	public List getRoot() {
		return root;
	}

	public void setRoot(List root) {
		this.root = root;
	}
	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
}
